import wepy from "wepy"


export const setStorageList = goods =>{
    //先从本地取出来购物车的加入购物车的列表，有就赋值，没有就取空数组；
    const goodsData = wepy.getStorageSync("GOOD") || {}
        //判断该商品存在就把数量累加，否则就新增一条数据
        if(goodsData[goods.id]){
            goodsData[goods.id] =goodsData[goods.id] + goods.count
        }else {
            goodsData[goods.id] = goods.count
        }

    wepy.setStorageSync("GOOD",goodsData)
}

export const updateStorageList = goods =>{
   // 1.取出原先保存的商品对象
  // localGoods取出来长成这个样子 {140:3,395:3}
  const localGoods = wepy.getStorageSync('GOOD') || {}

  // 修改数量
  localGoods[goods.goods_id] = goods.goods_number

  // 保存回去
  wepy.setStorageSync('GOOD', localGoods)
}

export const delGoods = goodsId =>{
    //先从本地取出来购物车的加入购物车的列表，有就赋值，没有就取空数组；
    const goodsData = wepy.getStorageSync("GOOD") || {};
    //直接根据商品ID从本地的商品对象里删除该商品
    delete goodsData[goodsId]
    //别忘记保存回去
    wepy.setStorageSync("GOOD",goodsData)
}